package org.noote.test_accelerometer;

import java.text.DecimalFormat;

import org.noote.libs.sensors.AccelerometerListener;
import org.noote.libs.sensors.AccelerometerManager;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class TestAccelerometer_Activity extends Activity implements AccelerometerListener {
	long prevtimeAcc = 0, prevtimeVel = 0, prevtimePos = 0;
	long timediff_interval = 100;
	float velmaxx=0,velmaxy=0,velmaxz=0;
	
	static DecimalFormat m_dfTwoDecimal = new DecimalFormat("#,##0.00");
	AccelerometerManager _accel = new AccelerometerManager();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (_accel.isSupported(this)) {
			_accel.startListening(this, this);
		}
		else Toast.makeText(this, "You haven't an accelerometer sensor !", Toast.LENGTH_LONG).show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (_accel.isListening()) {
			_accel.stopListening();
		}
	}    
	@Override
	public void onAccelerationChanged(float acc_x, float acc_y, float acc_z) {
		long curtime = System.currentTimeMillis();
		long timediff = curtime - prevtimeAcc;
		if (prevtimeAcc == 0) { timediff = 0; prevtimeAcc = curtime; return; }

		if(timediff > timediff_interval)
		{
			prevtimeAcc = curtime;
			// Acceleration
			((TextView)findViewById(R.id.txtAccX)).setText(m_dfTwoDecimal.format(acc_x) + " m/s^2");
			((TextView)findViewById(R.id.txtAccY)).setText(m_dfTwoDecimal.format(acc_y) + " m/s^2");
			((TextView)findViewById(R.id.txtAccZ)).setText(m_dfTwoDecimal.format(acc_z) + " m/s^2");
		}
	}
	
	@Override
	public void onVelocityChanged(float vel_x, float vel_y, float vel_z)
	{
		if(Math.abs(vel_x)>velmaxx) velmaxx=Math.abs(vel_x);
		if(Math.abs(vel_y)>velmaxy) velmaxy=Math.abs(vel_y);
		if(Math.abs(vel_z)>velmaxz) velmaxz=Math.abs(vel_z);
			
		long curtime = System.currentTimeMillis();
		long timediff = curtime - prevtimeVel;
		if (prevtimeVel == 0) { timediff = 0; prevtimeVel = curtime; return; }

		if(timediff > timediff_interval)
		{
			prevtimeVel = curtime;
			// Velocity
			((TextView)findViewById(R.id.txtVelX)).setText(m_dfTwoDecimal.format(vel_x) + " m/s");
			((TextView)findViewById(R.id.txtVelY)).setText(m_dfTwoDecimal.format(vel_y) + " m/s");
			((TextView)findViewById(R.id.txtVelZ)).setText(m_dfTwoDecimal.format(vel_z) + " m/s");

			// Velocity Max
			((TextView)findViewById(R.id.txtVelMaxX)).setText(m_dfTwoDecimal.format(velmaxx) + " m/s");
			((TextView)findViewById(R.id.txtVelMaxY)).setText(m_dfTwoDecimal.format(velmaxy) + " m/s");
			((TextView)findViewById(R.id.txtVelMaxZ)).setText(m_dfTwoDecimal.format(velmaxz) + " m/s");
		}
	}
	
	@Override
	public void onPositionChanged(float pos_x, float pos_y, float pos_z)
	{
		long curtime = System.currentTimeMillis();
		long timediff = curtime - prevtimePos;
		if (prevtimePos == 0) { timediff = 0; prevtimePos = curtime; return; }

		if(timediff > timediff_interval)
		{
			prevtimePos = curtime;
			
			// Position		
			((TextView)findViewById(R.id.txtPosX)).setText(m_dfTwoDecimal.format(pos_x) + " m");
			((TextView)findViewById(R.id.txtPosY)).setText(m_dfTwoDecimal.format(pos_y) + " m");
			((TextView)findViewById(R.id.txtPosZ)).setText(m_dfTwoDecimal.format(pos_z) + " m");
		}		
	}
	
	@Override
	public void onShake(float force)
	{
		Toast.makeText(this, "Shake on me ("+m_dfTwoDecimal.format(force)+"g)", Toast.LENGTH_SHORT).show();
	}
}